package com.wx.commons;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.wx.core.vo.MenuTree;
import com.wx.entity.WxMenu;
import com.wx.flag.WxMenuFlag;
import com.wx.vo.WxRootMenu;

public class WxMenuTreeUtil {
	/**
	 * TODO(得到子菜单对象集合) 作者:shanyz
	 */
	public List<WxMenu> getChild(Integer root_id, List<WxMenu> allList) {
		List<WxMenu> childList = new ArrayList<WxMenu>();
		for (WxMenu wxMenu : allList) {
			if (root_id == wxMenu.getPid().intValue()) {
				childList.add(wxMenu);
			}
		}
		return childList;
	}

	public int getRootNum(List<WxMenu> allList){
		int num = 0;
		for (WxMenu wxMenu : allList) {
			if(wxMenu.getPid() == 0)
				num++;
		}
		return num;
	}
	public int getChirldNum(int pid,List<WxMenu> allList){
		int num = 0;
		for (WxMenu wxMenu : allList) {
			if(wxMenu.getPid() == pid)
				num++;
		}
		return num;
	}
	
	/**
	 * TODO(得到菜单对象集合) 作者:shanyz
	 */
	public List<WxRootMenu> getRoot(List<WxMenu> allList) {
		List<WxRootMenu> rootList = new ArrayList<WxRootMenu>();
		for (WxMenu wxMenu : allList) {
			if (wxMenu.getPid() == 0) {//构建父节点对象
				WxRootMenu rootMenu = new WxRootMenu();
				BeanUtils.copyProperties(wxMenu, rootMenu);
				rootMenu.setChildList(getChild(wxMenu.getId(),allList));
				rootList.add(rootMenu);
			} 
		}
		return rootList;
	}
	/**
	 * TODO(将平台菜单对象转化为微信接口JSON对象) 作者:shanyz
	 */
	public MenuTree changeWxmenu(WxMenu wxMenu){
		MenuTree menu = new MenuTree();
		menu.setName(wxMenu.getmName());
		if(wxMenu.getType() == WxMenuFlag.TYPE_CLICK.getValue()){
			menu.setType("click");
			menu.setKey(wxMenu.getKeyId()+"_"+wxMenu.getKeyType());
		}
		
		if(wxMenu.getType() == WxMenuFlag.TYPE_CLICK_CUSTOMER_SERVICE.getValue()){
			menu.setType("click");
			menu.setKey("CUSTOMER_SERVICE");
		}
			
		if(wxMenu.getType() == WxMenuFlag.TYPE_VIEW.getValue()){
			menu.setType("view");
			menu.setUrl(wxMenu.getmHref());
		}
		return menu;
	}
	/**
	 * TODO(将平台菜单对象转化为微信接口JSON对象) 作者:shanyz
	 */
	public MenuTree changeWxmenu(WxRootMenu wxRootMenu){
		MenuTree menu = new MenuTree();
		menu.setName(wxRootMenu.getmName());
		if(wxRootMenu.getType() == WxMenuFlag.TYPE_CLICK.getValue()){
			menu.setType("click");
			menu.setKey(wxRootMenu.getKeyId()+"_"+wxRootMenu.getKeyType());
		}
		if(wxRootMenu.getType() == WxMenuFlag.TYPE_CLICK_CUSTOMER_SERVICE.getValue()){
			menu.setType("click");
			menu.setKey("CUSTOMER_SERVICE");
		}	
		if(wxRootMenu.getType() == WxMenuFlag.TYPE_VIEW.getValue()){
			menu.setType("view");
			menu.setUrl(wxRootMenu.getmHref());
		}
		return menu;
	}
	/**
	 * TODO排序 作者:shanyz
	 */
	public void sortRootList(List<WxRootMenu> rootList) {
		// 根节点排序
		Collections.sort(rootList, new Comparator<WxRootMenu>() {
			public int compare(WxRootMenu arg0, WxRootMenu arg1) {
				return arg0.getmOrder().compareTo(arg1.getmOrder());
			}
		});
		// 子节点排序
		for (WxRootMenu wxRootMenu : rootList) {
			if(wxRootMenu.getChildList() != null && wxRootMenu.getChildList().size() > 1){
				Collections.sort(wxRootMenu.getChildList(), new Comparator<WxMenu>() {
					public int compare(WxMenu arg0, WxMenu arg1) {
						return arg0.getmOrder().compareTo(arg1.getmOrder());
					}
				});
			}
		}
	}
	/**
	 * (转换微信JSON对象集合,排除 不显示的数据) 作者:shanyz
	 */
	public List<MenuTree> createWxMenu(List<WxMenu> allList){
		List<WxRootMenu> rootList = getRoot(allList);
		//排序
		sortRootList(rootList);
		//转换为微信菜单JSON对象
		List<MenuTree> wxList = new ArrayList<MenuTree>();
		for (WxRootMenu wxRootMenu : rootList) {
			//排序不显示的根节点
			if(wxRootMenu.getIsShow() == WxMenuFlag.MENU_NOT_SHOW.getValue())
				continue;
			MenuTree menu = new MenuTree();
			menu.setName(wxRootMenu.getmName());
			List<WxMenu> children = wxRootMenu.getChildList();
			if(children.size() > 0){//有子菜单
				List<MenuTree> wxList_child = new ArrayList<MenuTree>();
				for (WxMenu wxMenu : children) {
					//排序不显示的子节点
					if(wxMenu.getIsShow() == WxMenuFlag.MENU_NOT_SHOW.getValue())
						continue;
					MenuTree menu_child = changeWxmenu(wxMenu);
					wxList_child.add(menu_child);
				}
				//如果没有子菜单则设置为null，保证传给微信平台的JSON数据格式，设置根菜单的属性
				if(wxList_child.size() == 0){
					wxList_child = null;
					menu = changeWxmenu(wxRootMenu);
				}
				menu.setSub_button(wxList_child); 
			}else{
				menu = changeWxmenu(wxRootMenu);
			}
			wxList.add(menu);
		}
		return wxList;
	}
}
